#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include <QMessageBox>
#include "element.h"
//赵红波2016.10.14
DrawPCIRCLE::DrawPCIRCLE(void)
{
    m_drawType = PCIRCLE;
    m_state = DS_IDLE;
}

DrawPCIRCLE::~DrawPCIRCLE(void)
{

}

int DrawPCIRCLE::mouseRButtonPress(QPoint point)
{
    if( m_state == DS_IDLE )
    {
        m_start = point;
        m_state = DS_STATE1;
        return 1;
    }
    if(m_state == DS_STATE1)
    {
        m_middle = point;
        m_state = DS_STATE2;
        return 2;
    }
    if(m_state == DS_STATE2)
    {
        m_end = point;
        compute();
        return 3;
    }
    return 0;
}

int DrawPCIRCLE::mouseLButtonPress(QPoint point)
{
    if( m_state == DS_IDLE )
    {
        m_start = point;
        m_state = DS_STATE1;
        return 1;
    }
    if(m_state == DS_STATE1)
    {
        m_middle = point;
        m_state = DS_STATE2;
        return 2;
    }
    if(m_state == DS_STATE2)
    {
        m_end = point;
        compute();
        return 3;
    }
    return 0;
}

void DrawPCIRCLE::compute()
{
    double x1 = m_start.x();
    double y1 = m_start.y();
    double x2 = m_middle.x();
    double y2 = m_middle.y();
    double x3 = m_end.x();
    double y3 = m_end.y();

    double a, b, c, d, e, f, g, x, y;
    a = 2 * (x2 - x1);
    b = 2 * (y2 - y1);
    c = x2 * x2 + y2 * y2 - x1 * x1 - y1 * y1;
    d = 2 * (x3 - x2);
    e = 2 * (y3 - y2);
    f = x3 * x3 + y3 * y3 - x2 * x2 - y2 * y2;
    g = b * d - e * a;
    if(fabs(g) < 1e-5)
    {
        QMessageBox::information(NULL, "ERROR", "The three points can not build a circle,please set the points again!", QMessageBox::Yes , QMessageBox::Yes);
        m_state = DS_IDLE;
        return ;
    }
    x = (b * f - e * c) / g;
    y = (d * c - a * f) / g;

    m_radius = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
    m_centre = QPoint(x, y);
    m_state = DS_FINISH;
    m_textBox.moveTo(m_centre);
    updateText();
}

void DrawPCIRCLE::draw( QPainter &painter )
{
    if( m_state != Element::DS_IDLE )
    {
        painter.save();
        painter.setPen(m_linePen);
        int x, y, width, height;
        x = m_centre.x() - m_radius;
        y = m_centre.y() - m_radius;
        width = 2 * m_radius;
        height = 2 * m_radius;
        painter.drawArc ( x, y, width, height, 0, 16 * 360 );
        if( m_state == Element::DS_STATE1 )
        {
            painter.drawPoint(m_start);
        }
        else if( m_state == Element::DS_STATE2 )
        {
            QPoint points[2];
            points[0] = m_start;
            points[1] = m_middle;
            painter.drawPoints(points, 2);
        }
        else if( m_state == Element::DS_FINISH )
        {
            QPoint points[3];
            points[0] = m_start;
            points[1] = m_middle;
            points[2] = m_end;
            painter.drawPoints(points, 3);

            painter.drawLine(m_centre.x() - 5, m_centre.y(), m_centre.x() + 5, m_centre.y());
            painter.drawLine(m_centre.x(), m_centre.y() - 5, m_centre.x(), m_centre.y() + 5);

            painter.setFont(m_textFont);
            painter.drawText(m_textBox, m_text);
        }
        painter.restore();
    }

}
void DrawPCIRCLE::updateText(void)
{
    if(m_radius * m_scale >= 500.0)
    {
        m_text = QString::fromUtf8(u8"D = %1 mm").arg(2 * m_radius * m_scale / 1000.0, 2, 'f', 2);
    }
    else
    {
        m_text = QString::fromUtf8(u8"D = %1 μm").arg(2 * m_radius * m_scale, 2, 'f', 2);
    }
    QFontMetrics fm(m_textFont);
    int textWidth = fm.width(m_text);
    int textHeight = fm.height();
    m_textBox.setWidth(textWidth);
    m_textBox.setHeight(textHeight);
}

void DrawPCIRCLE::computeBox(void)
{

}

void DrawPCIRCLE::build(QPoint p1, QPoint p2, QPoint p3)
{
    m_start = p1;
    m_middle = p2;
    m_end = p3;
    compute();
}


